{extend name="index_layout"/}
{block name="main"}
<form class="layui-form" method="post">
    <div class="zTreeDemoBackground left">
        <ul class="ztree" style="margin-left: 5px;margin-top:5px; padding: 0;">
            <li><a title="全部展开、折叠 "><span class="button ico_open"></span><span id="ztree_expandAll">全部展开、折叠 </span></a> </li>
        </ul>
        <ul id="treeDemo" class="ztree"></ul>
    </div>
    {:token()}
    <input type="hidden" name="row[rules]" value="" />
    <input type="hidden" name="row[id]" value="{$data.id}" />
    <input type="hidden" name="row[parentid]" value="{$data.parentid}">
    <div class="layui-form-item layer-footer">
        <div class="">
            <button class="layui-btn" lay-submit>立即提交</button>
        </div>
    </div>
</form>
{/block}
{block name="script"}
<script>
layui.use(['jquery', 'ztree','yznForm'], function() {
    var $ = layui.$,
        yznForm = layui.yznForm;
    //配置
    var setting = {
        //设置 zTree 的节点上是否显示 checkbox / radio
        check: {
            enable: true,
            chkboxType: { "Y": "ps", "N": "ps" }
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentid",
            }
        },
        callback: {
            beforeClick: function(treeId, treeNode) {
                if (treeNode.isParent) {
                    zTree.expandNode(treeNode);
                    return false;
                } else {
                    return true;
                }
            },
            /*onClick: function(event, treeId, treeNode) {
                //栏目ID
                var catid = treeNode.catid;
                //保存当前点击的栏目ID
                setCookie('tree_catid', catid, 1);
            }*/
        }
    };
    //节点数据
    var zNodes = {$nodeList|raw};
    //zTree对象
    var zTree = null;
    $(document).ready(function() {
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        zTree = $.fn.zTree.getZTreeObj("treeDemo");
        zTree.expandAll(true);
        $("#ztree_expandAll").click(function() {
            if ($(this).data("open")) {
                zTree.expandAll(false);
                $(this).data("open", false);
            } else {
                zTree.expandAll(true);
                $(this).data("open", true);
            }
        });
    });

    $('.layui-btn').click(function() {
        _form = $('.layui-form');
        //处理被选中的数据
        _form.find('input[name="rules"]').val("");
        var nodes = zTree.getCheckedNodes(true);
        var str = "";
        $.each(nodes, function(i, value) {
            if (str != "") {
                str += ",";
            }
            str += value.id;
        });
        _form.find('input[name="row[rules]"]').val(str);
        yznForm.bindevent($("form.layui-form"));
    })
});
</script>
{/block}
